define(["esri/core/declare",
        'dojo/_base/lang',
        "./script/Graphic.js",
        "esri/geometry/Point",
        "esri/geometry/Polyline",
        "esri/geometry/Polygon",
        "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
        "esri/layers/GraphicsLayer",
        'dojo/_base/array',
        './script/MathStuff.js',
        "esri/geometry/geometryEngine",
        './script/three.min.js',
        "esri/views/3d/externalRenderers",
        "esri/symbols/PolygonSymbol3D",
        "esri/symbols/ExtrudeSymbol3DLayer",
        "esri/symbols/PictureMarkerSymbol",
        "esri/symbols/TextSymbol",
        "esri/layers/FeatureLayer",
        "esri/geometry/support/webMercatorUtils"
    ],
    function (declare, lang, Graphic, Point, Polyline, Polygon, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
              GraphicsLayer, Array, MathStuff, geometryEngine, THREE, externalRenderers, PolygonSymbol3D, ExtrudeSymbol3DLayer,
              PictureMarkerSymbol,
              TextSymbol,
              FeatureLayer,
              webMercatorUtils) {
        var that = null;
        var draw = declare(null, {
            declaredClass: "DrawExt",
            constructor: function (options) {
                that = this;

                that.view = options.view
                that.gl = options.gl
                that.map = options.map;

                that.worldElevation = options.worldElevation;

                that.glText = options.glText;
                that.pointArr = [];//点图标数组

                that.curMarkers = options.curMarkers;//传入的marker对象

                that.dotFields = [];
                var fItem1 = {
                    name: 'id',
                    alias: 'id',
                    type: 'oid'
                };
                var fItem2 = {
                    name: 'name',
                    alias: 'name',
                    type: 'string'
                };
                var fItem3 = {
                    name: 'type',
                    alias: 'type',
                    type: 'string'
                };
                var fItem4 = {
                    name: 'code',
                    alias: 'code',
                    type: 'string'
                }
                var fItem5 = {
                    name: 'url',
                    alias: 'url',
                    type: 'string'
                }

                var fItem6 = {
                    name: 'memo',
                    alias: 'memo',
                    type: 'string'
                }

                that.renderArr = [];//用于展示图标的数组

                that.dotFields.push(fItem1);
                that.dotFields.push(fItem2);
                that.dotFields.push(fItem3);
                that.dotFields.push(fItem4);
                that.dotFields.push(fItem5);
                that.dotFields.push(fItem6);

                var picArr2 = ["./img/fire0.png",
                    "./img/fire1.png",
                    "./img/fire2.png",
                    "./img/fire3.png",
                ];
                var width = 28, height = 38;

                that.fireSymArr = EsGraphicSpriteHelper.initSymls(picArr2, width, height);

                that.pointSymbol = options.pointSymbol || new SimpleMarkerSymbol({
                    color: [226, 119, 40],

                    outline: {
                        color: [255, 255, 255],
                        width: 2
                    }
                })
                that.lineSymbol = options.lineSymbol || new SimpleLineSymbol({
                    color: [226, 119, 40],
                    width: 4
                })
                that.fillSymbol = options.fillSymbol || new SimpleFillSymbol({
                    color: [227, 139, 79, 0.8],
                    outline: {
                        color: [255, 255, 255],
                        width: 1
                    }
                })
                that._positions = []
                that._points = []
                that._preGra = null
                that._currGra = null
                that._newHead = false
                that._lastGeometry = null
                that._headerCollection = []
                that._handlers = {}
                that._currMessage = '';
                that._curIco = '';//当前加载的点图标
                document.body.insertAdjacentHTML('beforeend', '<div id="tips" style="position:absolute;padding: 20px; display:none;background: white;z-index: 2147483647;font-size: 12px;border-style: solid;border-color: #dddddd;border-width: 1px;margin-top: 15px;margin-left: 15px;padding-bottom: 5px;padding-left: 5px;padding-right: 5px;padding-top: 5px;"></div>')
                that.view.root.addEventListener('mousemove', function (e) {
                    if (that._currMessage) {
                        var left = e.clientX + "px";
                        var top = e.clientY + "px";

                        var div = document.getElementById('tips');
                        that._currMessage = that._currMessage.replace("$$", "<img src='" + that._curIco + "' class='tipIcon'/>");
                        div.innerHTML = that._currMessage;

                        div.style.left = left;
                        div.style.top = top;

                        div.style.display = 'block'
                    }
                }.bind(this));
            },
            reset: function (mode) {
                that.gl.elevationInfo = {mode: mode}
                that._points.length = 0
                that._preGra = null
                that._lastGeometry = null
                that._currGra = null

                that._specialSymbol = null;//modify by tms,2017-12-19
            },
            resetOnly: function (mode) {
                that.gl.elevationInfo = {mode: mode}

                that._currGra = null

                that._specialSymbol = null;//modify by tms,2017-12-19
            },
            clearAllGraphic: function () {
                //清除所有图形
                that.reset();
                that.gl.removeAll();
                that.glText.removeAll();
                //that.glIcon.removeAll();
                map.remove(that.featureLayer)
            }
            , setIcon: function (iconUrl) {
                that._curIco = iconUrl;

            }
            ,
            addGraphic2Map: function (geometry, symbol, type) {
                that._preGra = that._currGra;

                if (that._specialSymbol != null && that._points.length >= 3) {
                    that._currGra = new Graphic({
                        geometry: geometry,
                        symbol: that._specialSymbol
                    });

                } else {
                    that._currGra = new Graphic({
                        geometry: geometry,
                        symbol: symbol
                    });
                }

                var guid = that.newGuid();
                //生成唯一的name
                that._currGra.attributes = {
                    "type": type,
                    "name": type + guid

                };

                //console.log('addGraphic2Map')
                that.gl.remove(that._preGra);
                that.gl.add(that._currGra);

            },
            activate: function (options) {
                //that._currGra = null

                that.setCurrentHis(options);
                switch (options) {
                    case draw.POINT:
                        that.reset('on-the-ground')

                        that._currMessage = draw.messageTemplate.singleClickAdd
                        that._handlers.click = that.view.on('click', that.draw_pt_click.bind(this));
                        break;
                    case draw.PTICON:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleIconAdd;
                        that._handlers.click = that.view.on('click', that.draw_pticon_click.bind(this));
                        break;
                    case draw.FREELINE:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawFreeLine_click.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawFreeLine_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.LINE:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart

                        that._handlers.click = that.view.on('click', that.drawline_click.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawline_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;

                    case draw.POLYGON:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawPolygon.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawPolygon_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.CIRCLE:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawCircle.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawCircle_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.CURVE:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawCurve.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawCurve_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.FREEHAND_ARROW:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawFreeHandArrow.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawFreeHandArrow_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.ATTACK_ARROW:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawAttackArrow.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawAttackArrow_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this));
                        break;
                    case draw.BEZIER_POLYGON:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawFreePolygon.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawFreePolygon_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.BEZIER_CURVE:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawbezierLine.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawbezierLine_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.MULTIHEAD:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawMultiHead.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawMultiHead_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.DASHLINE:
                        that.reset('on-the-ground')
                        that.drawHandler = that.view.on('click', that.drawDash.bind(this));
                        break;
                    case draw.CUBE:
                        // that.reset('on-the-ground')
                        //that.activate(draw.POLYGON)
                        that._currMessage = draw.messageTemplate.clickCubic
                        that._handlers.click = that.view.on('click', that.drawcube_click.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawcube_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                    case draw.FIRELINE:
                        that.reset('on-the-ground')
                        that._groupKey = that.newGuid();//生成一个一组的guid

                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawbezierLineFire.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawbezierLineFire_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))

                        break;
                    case draw.FIREAREA:
                        //火场

                        that.reset('on-the-ground');
                        //定义不现的填充
                        that._specialSymbol = new SimpleFillSymbol({
                            color: [30, 30, 30, 0.8],
                            outline: {
                                color: [255, 0, 0],
                                width: 2
                            }
                        });

                        that._currMessage = draw.messageTemplate.singleClickStart
                        that._handlers.click = that.view.on('click', that.drawFreePolygon.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawFreePolygon_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))

                        break;
                    case draw.DMZ:
                        that.reset('on-the-ground')
                        that._currMessage = draw.messageTemplate.singleClickStart

                        //定义不现的填充
                        that._specialSymbol = new SimpleFillSymbol({
                            color: [0, 255, 0, 0.8],
                            outline: {
                                color: [0, 255, 0],
                                width: 2
                            }
                        });
                        that._handlers.click = that.view.on('click', that.drawPolygon.bind(this));
                        that._handlers.pointer_move = that.view.on('pointer-move', that.drawPolygon_pt_move.bind(this))
                        that._handlers.doubleClick = that.view.on('double-click', that.draw_double_click.bind(this))
                        break;
                }
            },

            //初始化当前的历史记录数据。
            setCurrentHis: function (type) {
                that._curGraphic = new Object()
                that._curGraphic.id = that.newGuid()
                that._curGraphic.type = type
                that._curGraphic.points = []
                that._curGraphic.gpLayerItem = []
                that._curGraphic.positions = []
                //todo

            },
            deactivate: function () {



                //触发弹出层，要求输入对象名称
                $('#fireCommandDlg').modal('show');

            },
            drawTextSym: function () {
                //保存到文本图层
                var text = $("#fireCommnad-name").val();

                $("#fireCommandDlg").modal("hide");
                //text=text.trim();
                if (text.length > 0) {

                    var point = that._lastPoint;

                    var queryL = that.worldElevation.queryElevation(
                        point, {returnSampleInfo: true});
                    console.log(point.z);
                    var diff = 0;
                    // When both query promises resolve execute the following code
                    //all([])
                    queryL.then(function (rs) {
                        var pos = rs.geometry;

                        point.z = pos.z;
                        point.z += 2;

                        console.log(point.z);

                        //准备显示的信息
                        var symbolText = TextSymbol({
                            text: text,
                            color: "white",
                            haloSize: 1,
                            haloColor: "black"
                        });

                        var txtGraphic = Graphic({
                            geometry: point,
                            symbol: symbolText, // ,,markerSymbol
                            //popupTemplate: template
                        });

                        that.glText.add(txtGraphic);

                        that._curGraphic.gpLayerItem.push({
                            layerId: glText,
                            graphicItem: txtGraphic
                        });

                    });

                }

                //clear all handler

                for (var key in that._handlers) {
                    that._handlers[key].remove()
                }
                // that.drawHandler.remove()
                that._preGra = null
                //that._lastGeometry = null
                that._y = 0
                that._yFinal = 0
                that._currMessage = ''
                var div = document.getElementById('tips');
                div.style.display = 'none'
                that._currGra = null;

                $("#fireCommnad-name").val("");

                //保存到全局结构中
                that.curMarkers.graphics.push(that._curGraphic);

            },

            draw_pt_click: function (e) {

                that.draw_pt_show(e.mapPoint);

                // that.reset('on-the-ground')
            },
            draw_pt_show: function (point, isRedraw) {

                var point = new Point({
                    x: point.x,
                    y: point.y,
                    z: point.z,
                    spatialReference: that.view.spatialReference
                })
                if (isRedraw) {

                } else {
                    that._curGraphic.points.push({
                        x: point.x,
                        y: point.y,
                        z: point.z
                    });
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: point
                    });

                }

                that._lastPoint = point;//保存最后的一个点，备用

                that.addGraphic2Map(point, that.pointSymbol, draw.POINT)

                if (isRedraw) {

                } else {
                    that.deactivate()
                }

            },

            draw_pticon_click: function (e) {
                var item = e.mapPoint;
                //处理后面的meida数据
                var dotLen = 0;
                dotLen = that.pointArr.length;
                var newItem = {
                    id: dotLen + 1,
                    name: '新的项',
                    type: 'point',
                    code: 'tempIcon', //临时定位图标
                    url: './img/蓝色标记.png'
                }
                dotLen++;
                that.draw_pticon_show(item, newItem)
                that.deactivate()
            },
            draw_pticon_show: function (item, newItem, iReDraw) {
                var point = new Point({
                    latitude: item.latitude,
                    longitude: item.longitude,
                    z: item.z
                });

                that._lastPoint = point;//保存最后的一个点，备用

                that.genRenderArr(newItem.code, newItem.url);//将代码和链接放入，后面好按它进行渲染

                that.pointArr.push({
                    geometry: point,
                    attributes: newItem
                });

                that.showIconLayer()
                //非重绘状态，保存数据
                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                    //that._points = points;

                } else {
                    that._curGraphic.points = []
                    that._curGraphic.points.push(item)
                    that._curGraphic.newItem = []
                    that._curGraphic.newItem.push(newItem)

                    that._curGraphic.pticonLayerItem = []
                    that._curGraphic.pticonLayerItem.push({
                        layerId: map,
                        graphicItem: that.featureLayer
                    })
                }
            },
            genRenderArr: function (key, url) {

                if (that.renderArr.length > 0) {
                    //查询是否存在
                    var isExit = false;
                    for (var i = 0; i < that.renderArr.length; i++) {
                        if (that.renderArr[i].value == key) {
                            isExit = true;
                            return;
                        }
                    }

                }

                //放入
                that.renderArr.push({
                    value: key,
                    symbol: that.getUniqueValueSymbol(url)
                })

                return that.renderArr;

            },
            getUniqueValueSymbol: function (name, color) {
                // The point symbol is visualized with an icon symbol. To clearly see the location of the point
                // we displace the icon vertically and add a callout line. The line connects the offseted symbol with the location
                // of the point feature.
                return {
                    type: "point-3d", // autocasts as new PointSymbol3D()
                    symbolLayers: [{
                        type: "icon", // autocasts as new IconSymbol3DLayer()
                        resource: {
                            href: name
                        },
                        size: 20,
                        outline: {
                            color: "white",
                            size: 2
                        }
                    }],

                    verticalOffset: {
                        screenLength: 40,
                        maxWorldLength: 200,
                        minWorldLength: 35
                    },

                    callout: {
                        type: "line", // autocasts as new LineCallout3D()
                        color: "white",
                        size: 2,
                        border: {
                            color: color
                        }
                    }
                };
            },

            pointsRenderer: function (fieldShow) {

                if (typeof(fieldShow) == "undefined" || fieldShow == "") {
                    fieldShow = "ftKey";
                }

                var uPointer = {
                    type: "unique-value", // autocasts as new UniqueValueRenderer()
                    field: fieldShow,
                    uniqueValueInfos: that.renderArr
                };

                return uPointer;

            },

            showIconLayer: function () {
                //显示当前FeatureLayer中的数据

                if (that.featureLayer) {
                    map.remove(that.featureLayer);
                    that.featureLayer = null;
                    console.log("移除对应的featurelayer");
                }

                //生成对应的显示层
                var newL = new FeatureLayer({
                    fields: that.dotFields,
                    id: '_commandFireIcon',
                    objectIdField: "id",

                    title: '防火指挥图标层',
                    geometryType: "point",

                    elevationInf0o: {
                        // elevation mode that will place points on top of the buildings or other SceneLayer 3D objects
                        mode: "relative-to-scene"
                    },
                    renderer: that.pointsRenderer("code"),

                    outFields: ["*"],
                    // feature reduction is set to selection because our scene contains too many points and they overlap
                    featureReduction: {
                        type: "selection"
                    },
                    labelingInfo: [{
                        // When using callouts on labels, "above-center" is the only allowed position
                        labelPlacement: "above-center",
                        labelExpressionInfo: {
                            value: "{name}"
                        },
                        symbol: {
                            type: "label-3d", // autocasts as new LabelSymbol3D()
                            symbolLayers: [{
                                type: "text", // autocasts as new TextSymbol3DLayer()
                                material: {
                                    color: "black"
                                },
                                halo: {
                                    color: [255, 255, 255, 0.7],
                                    size: 2
                                },
                                size: 10
                            }],
                            // Labels need a small vertical offset that will be used by the callout
                            verticalOffset: {
                                screenLength: 150,
                                maxWorldLength: 2000,
                                minWorldLength: 30
                            },
                            // The callout has to have a defined type (currently only line is possible)
                            // The size, the color and the border color can be customized
                            callout: {
                                type: "line", // autocasts as new LineCallout3D()
                                size: 0.5,
                                color: [0, 0, 0],
                                border: {
                                    color: [255, 255, 255, 0.7]
                                }
                            }
                        }
                    }],
                    labelsVisible: true

                });

                newL.source = that.pointArr;

                map.add(newL);

                that.featureLayer = newL;
            },

            drawline_click: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = [e.mapPoint.x, e.mapPoint.y, e.mapPoint.z]
                that._points.push(pt)
                if (that._points.length >= 2) {
                    var line = new Polyline({
                        paths: [that._points],
                        spatialReference: that.view.spatialReference
                    })
                    that.addGraphic2Map(line, that.lineSymbol)
                }
            },
            drawline_pt_move: function (e) {

                if (that._points.length == 0) return
                //if (e.x == that.clickx && e.y == that.clicky) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                // console.log(mp)
                var pt = [mp.x, mp.y, mp.z]
                if (that._points.length == 1) that._points.push(pt)
                else {
                    // if (that._points.length <= that.clickCount)
                    // {
                    //     console.log('小于！')
                    //     return
                    // }
                    that._points.pop()
                    that._points.push(pt)
                }
                // console.log(that._points.length)
                // that._points.splice(that._points.length - 1, 1, pt);
                if (that._points.length >= 2) {
                    var line = new Polyline({
                        paths: [that._points],
                        spatialReference: that.view.spatialReference
                    })
                    that.addGraphic2Map(line, that.lineSymbol)

                }
            },
            draw_double_click: function (e) {

                console.log("double end...");
                var point = new Point({
                    x: e.mapPoint.x,
                    y: e.mapPoint.y,
                    z: e.mapPoint.z,
                    spatialReference: that.view.spatialReference
                })
                if (that._lastPoint == null) {

                    that._lastPoint = point;//保存最后的一个点，备用

                }

                e.stopPropagation();
                that.deactivate()
            },
            //随手画
            drawFreeLine_click: function (e) {
                that._currMessage = draw.messageTemplate.dblclickend
                var pt = [e.mapPoint.x, e.mapPoint.y, e.mapPoint.z]
                that._points.push(pt)
                if (that._points.length >= 2) {
                    that.drawFreeLine_show(that._points);
                }
            },
            drawFreeLine_pt_move: function (e) {
                if (that._points.length == 0) return
                var mp = that.view.toMap({x: e.x, y: e.y})

                // console.log(mp)
                var pt = [mp.x, mp.y, mp.z]
                that._points.push(pt)
                if (that._points.length >= 2) {
                    that.drawFreeLine_show(that._points);
                }
            },
            drawFreeLine_show: function (points, iReDraw) {

                var line = new Polyline({
                    paths: [points],
                    spatialReference: that.view.spatialReference
                });
                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                    //that._points = points;

                } else {

                    //that._points
                    that._curGraphic.points = [];
                    //that._curGraphic.points=points;
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);

                    });

                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: line
                    });

                }

                that.addGraphic2Map(line, that.lineSymbol, draw.FREELINE);

            },
            //隔离带
            drawPolygon: function (e) {

                var pt = [e.mapPoint.x, e.mapPoint.y, e.mapPoint.z]
                that._points.push(pt)
                if (that._points.length == 1) {
                    that._currMessage = draw.messageTemplate.clickdblclick
                }
                that.drawPolygon_pt_show(that._points, true)
            },
            drawPolygon_pt_move: function (e) {
                if (that._points.length == 0) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = [mp.x, mp.y, mp.z]
                if (that._points.length == 1) {
                    that._points.push(pt)
                    that.drawPolygon_pt_show(that._points)
                }

                else if (that._points.length == 2) {
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawPolygon_pt_show(that._points)
                }
                else {
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawPolygon_pt_show(that._points)
                }

            },
            drawPolygon_pt_show: function (points, isClick, iReDraw) {
                if (points.length >= 3) {
                    var r = JSON.parse(JSON.stringify(points))
                    r.push(r[0])

                    var polygon = new Polygon({
                        rings: r,
                        spatialReference: that.view.spatialReference
                    })
                    that._lastPoint = polygon.centroid;//中心点
                    that.addGraphic2Map(polygon, that.fillSymbol)
                }
                if (!isClick) {
                    if (points.length == 1) {
                        var line = new Polyline({
                            paths: [points],
                            spatialReference: that.view.spatialReference
                        })
                        that.addGraphic2Map(line, that.lineSymbol)
                    }

                    else if (points.length == 2) {
                        line = new Polyline({
                            paths: [points],
                            spatialReference: that.view.spatialReference
                        })
                        that._lastPoint = points[0];
                        that.addGraphic2Map(line, that.lineSymbol)
                    }
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                } else {
                    that._curGraphic.points = [];
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);

                    });

                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: polygon || line
                    });

                }
            },

            drawCircle: function (e) {
                that._currMessage = draw.messageTemplate.dblclickend
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)
                if (that._points.length >= 2) {
                    var geometry = new Polygon({
                        rings: [MathStuff.circle(that._points[0], that._points[that._points.length - 1])],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.fillSymbol)
                }
            },
            drawCircle_pt_move: function (e) {
                if (that._points.length == 0) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}
                that._points.push(pt)
                if (that._points.length >= 1) {
                    var geometry = new Polygon({
                        rings: [MathStuff.circle(that._points[0], that._points[that._points.length - 1])],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.fillSymbol)
                }
            },

            drawCurve: function (e) {
                var pt = [e.mapPoint.x, e.mapPoint.y, e.mapPoint.z]
                that._points.push(pt)
                if (that._points.length == 1) {
                    that._currMessage = draw.messageTemplate.clickAddSecond
                }
                else if (that._points.length == 2) {
                    that._currMessage = draw.messageTemplate.dblclickend
                }
                if (that._points.length >= 3) {
                    var r = JSON.parse(JSON.stringify(that._points));
                    var geometry = new Polyline({
                        paths: [MathStuff.curve(r[0], r[1], r[r.length - 1])],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.lineSymbol)
                }
            },
            drawCurve_pt_move: function (e) {
                if (that._points.length < 2) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = [mp.x, mp.y, mp.z]
                that._points.push(pt)
                if (that._points.length >= 3) {
                    var r = JSON.parse(JSON.stringify(that._points));
                    var geometry = new Polyline({
                        paths: [MathStuff.curve(r[0], r[1], r[r.length - 1])],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.lineSymbol)
                }
            },
            // 自由箭头
            drawFreeHandArrow: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)

                that.drawFreeHandArrow_show(that._points, true);
            },
            drawFreeHandArrow_pt_move: function (e) {
                if (that._points.length < 1) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}
                that._points.push(pt);

                that.drawFreeHandArrow_show(that._points, false);//非点击

            },
            drawFreeHandArrow_show: function (points, isClick, iReDraw) {

                var polygon

                if (points.length == 2) {
                    if (!isClick) {
                        points.splice(1, 1, pt);
                    }

                    polygon = new Polygon({
                        rings: [MathStuff.arrow2(points[0], points[1])],
                        spatialReference: that.view.spatialReference
                    })

                    that.addGraphic2Map(polygon, that.fillSymbol)

                }
                else if (points.length > 2) {
                    if (!isClick) {
                        points.splice(points.length - 2, 1)
                    }

                    polygon = new Polygon({
                        rings: [MathStuff.arrow3(points)],
                        spatialReference: that.view.spatialReference
                    })

                    that.addGraphic2Map(polygon, that.fillSymbol)
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {

                    // that._points=points;
                } else {

                    //that._points
                    that._curGraphic.points = [];
                    //that._curGraphic.points=points;
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);

                    });

                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: polygon
                    });

                }

            },
            //进攻箭头
            drawAttackArrow: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)
                that.drawAttackArrow_pt_show(that._points, true)
            },
            drawAttackArrow_pt_move: function (e) {
                if (that._points.length < 1) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}

                that._points.push(pt)
                that.drawAttackArrow_pt_show(that._points, false)
            },
            drawAttackArrow_pt_show: function (points, isClick, iReDraw) {
                var polygon
                if (points.length == 2) {
                    if (!isClick) {
                        points.splice(1, 1, pt)
                    }
                    polygon = new Polygon({
                        rings: [MathStuff.arrow4(points[0], points[1])],
                        spatialReference: that.view.spatialReference
                    })

                    that.addGraphic2Map(polygon, that.fillSymbol)
                }
                else if (points.length > 2) {
                    if (!isClick) {
                        points.splice(points.length - 2, 1)
                    }
                    polygon = new Polygon({
                        rings: [MathStuff.arrow5(points)],
                        spatialReference: that.view.spatialReference
                    })

                    that.addGraphic2Map(polygon, that.fillSymbol)
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {

                    // that._points=points;
                } else {
                    that._curGraphic.points = []
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point)
                    })

                    that._curGraphic.gpLayerItem = []
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: polygon
                    })

                }
            },
            //着火区域
            drawFreePolygon: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)
                that.drawFreePolygon_pt_show(that._points, true)
            },
            drawFreePolygon_pt_move: function (e) {
                if (that._points.length == 0) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}
                if (that._points.length == 1) {
                    that._points.push(pt)
                    that.drawFreePolygon_pt_show(that._points)
                }
                else if (that._points.length == 2) {
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawFreePolygon_pt_show(that._points)

                }
                else {
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawFreePolygon_pt_show(that._points)

                }

            },
            drawFreePolygon_pt_show: function (points, isClick, iReDraw) {
                if (points.length >= 3) {
                    var geometry = new Polygon({
                        rings: [MathStuff.bezierPoly(points)],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.fillSymbol)
                }
                if (!isClick) {
                    if (points.length == 1) {
                        var line = new Polyline({
                            paths: [points],
                            spatialReference: that.view.spatialReference
                        })
                        that.addGraphic2Map(line, that.lineSymbol)
                    }
                    else if (points.length == 2) {
                        line = new Polyline({
                            paths: [points],
                            spatialReference: that.view.spatialReference
                        })
                        that.addGraphic2Map(line, that.lineSymbol)
                    }
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                } else {
                    that._curGraphic.points = [];
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);

                    });

                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: geometry || line
                    });

                }
            },

            drawbezierLine: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)
                if (that._points.length >= 3) {
                    var geometry = new Polyline({
                        paths: [MathStuff.bezierLine(that._points)],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.lineSymbol)
                }
            },
            drawbezierLine_pt_move: function (e) {
                if (that._points.length <= 1) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}
                if (that._points.length == 2) that._points.push(pt)
                else {
                    that._points.splice(that._points.length - 1, 1, pt)
                }
                var geometry = new Polyline({
                    paths: [MathStuff.bezierLine(that._points)],
                    spatialReference: that.view.spatialReference
                });
                that.addGraphic2Map(geometry, that.lineSymbol)
            },
            newGuid: function () { //方法一：
                var guid = "";
                var n = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                for (var i = 1; i <= 8; i++) {
                    guid += n;
                }
                return guid;
            },
            drawbezierLineFire: function (e) {
                that._currMessage = draw.messageTemplate.clickdblclick
                var pt = {
                    x: e.mapPoint.x,
                    y: e.mapPoint.y,
                    z: e.mapPoint.z,
                    lot: e.mapPoint.longitude,
                    lat: e.mapPoint.latitude
                }
                // var ps = {lot: e.mapPoint.longitude, lat: e.mapPoint.latitude}
                // that._positions.push(ps)
                that._points.push(pt)
                that.drawbezierLineFire_pt_show(that._points, true)
            },
            drawbezierLineFire_pt_move: function (e) {
                if (that._points.length <= 1) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {
                    x: mp.x,
                    y: mp.y,
                    z: mp.z
                    // lot: mp.longitude,
                    // lat: mp.latitude
                }
                if (that._points.length == 2) that._points.push(pt)
                else {
                    pt.lot = that._points[that._points.length - 1].lot
                    pt.lat = that._points[that._points.length - 1].lat
                    that._points.splice(that._points.length - 1, 1, pt)
                }
                that.drawbezierLineFire_pt_show(that._points)
            },
            drawbezierLineFire_pt_show: function (points, isClick, iReDraw) {
                if (!isClick) {
                    var geometry = new Polyline({
                        paths: [MathStuff.bezierLine(points)],
                        spatialReference: that.view.spatialReference
                    });
                    that.addGraphic2Map(geometry, that.lineSymbol)
                } else if (isClick) {
                    if (points.length >= 3) {
                        var geometry = new Polyline({
                            paths: [MathStuff.bezierLine(points)],
                            spatialReference: that.view.spatialReference
                        });
                        that.addGraphic2Map(geometry, that.lineSymbol);
                    }
                    //加入火
                    points.forEach(function (position) {
                        if (position.lot && position.lat) {
                            var guid = that.newGuid();
                            var sp0 = new EsGraphicSprite(guid, position.lot, position.lat, that.fireSymArr, 10, 1, that._groupKey);
                            timerAnimate.AddQueue(sp0);
                        }
                    })
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                } else {
                    that._curGraphic.points = [];
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);
                    });
                    console.log(that._curGraphic.points)
                    // if (ps != undefined) {
                    //     that._curGraphic.positions.push(ps[ps.length - 1])
                    // }
                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: geometry
                    });
                }
            },

            drawMultiHead: function (e) {
                that._currMessage = draw.messageTemplate.clickdblright
                if (e.native.which == 3) {
                    that.newHead()
                    return
                }
                var pt = {x: e.mapPoint.x, y: e.mapPoint.y, z: e.mapPoint.z}
                that._points.push(pt)

                if (that._points.length == 1) return
                if (that._newHead) {
                    that._newHead = false
                    that._points = [that._points[0], pt]
                }
                that.drawMultiHead_pt_show(that._points, true)
            },
            drawMultiHead_pt_move: function (e) {
                if (that._points.length <= 0) return
                var mp = that.view.toMap({x: e.x, y: e.y})
                var pt = {x: mp.x, y: mp.y, z: mp.z}

                if (that._newHead) {
                    that._newHead = false
                    that._points = [that._points[0], pt]
                }
                if (that._points.length == 1) {
                    that._points.push(pt)
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawMultiHead_pt_show(that._points)
                }
                else if (that._points.length > 1) {
                    that._points.splice(that._points.length - 1, 1, pt)
                    that.drawMultiHead_pt_show(that._points)
                }
            },
            drawMultiHead_pt_show: function (points, isClick, iReDraw) {
                var polygon

                if (isClick) {
                    if (that._points.length == 2) {
                        polygon = new Polygon({
                            rings: [MathStuff.arrow2(that._points[0], that._points[1])],
                            spatialReference: that.view.spatialReference
                        })
                    }
                    else if (that._points.length > 2) {
                        polygon = new Polygon({
                            rings: [MathStuff.arrow3(that._points)],
                            spatialReference: that.view.spatialReference
                        })
                    }
                    if (that._headerCollection.length == 0)
                        that._headerCollection.push(polygon)
                    else
                        that._headerCollection[that._headerCollection.length - 1] = polygon
                    var union = geometryEngine.union(that._headerCollection);
                    if (!union) {
                        that._points.pop()
                        polygon = new Polygon({
                            rings: [MathStuff.arrow2(that._points[0], that._points[1])],
                            spatialReference: that.view.spatialReference
                        })
                        if (that._headerCollection.length == 0)
                            that._headerCollection.push(polygon)
                        else
                            that._headerCollection[that._headerCollection.length - 1] = polygon
                        union = geometryEngine.union(that._headerCollection);
                        if (union) {
                            polygon = new Polygon({
                                rings: [MathStuff.arrow3(that._points)],
                                spatialReference: that.view.spatialReference
                            })
                            if (that._headerCollection.length == 0)
                                that._headerCollection.push(polygon)
                            else
                                that._headerCollection[that._headerCollection.length - 1] = polygon
                            union = geometryEngine.union(that._headerCollection);
                        }
                    }
                    that.addGraphic2Map(union, that.fillSymbol)
                } else if (!isClick) {
                    if (that._points.length == 1) {
                        polygon = new Polygon({
                            rings: [MathStuff.arrow2(that._points[0], that._points[1])],
                            spatialReference: that.view.spatialReference
                        })
                    }
                    else if (that._points.length > 1) {
                        polygon = new Polygon({
                            rings: [MathStuff.arrow3(that._points)],
                            spatialReference: that.view.spatialReference
                        })
                    }
                    if (that._headerCollection.length == 0)
                        that._headerCollection.push(polygon)
                    else
                        that._headerCollection[that._headerCollection.length - 1] = polygon
                    var union = geometryEngine.union(that._headerCollection);
                    that.addGraphic2Map(union, that.fillSymbol)
                }

                if (typeof (iReDraw) != 'undefined' && iReDraw) {
                } else {
                    that._curGraphic.points = [];
                    points.forEach(function (point) {
                        that._curGraphic.points.push(point);
                    });

                    that._curGraphic.gpLayerItem = [];
                    that._curGraphic.gpLayerItem.push({
                        layerId: that.gl,
                        graphicItem: polygon
                    });

                }
            }
            ,

            _y: 0,
            _yFinal:
                0,
            drawcube_click: function (e) {
                // that.view.hitTest({x: e.x, y: e.y}).then(function (response) {
                //     var result = response.results[0];
                //
                //     //console.log(response.results);
                //     if (result && result.graphic) {
                //         //objids.push(result.graphic.attributes.objectid)
                //         that._yFinal = that._y;
                //         that.gl.remove(result.graphic)
                //
                //     }
                // }.bind(this))
                that._currMessage = draw.messageTemplate.dblclickend
                var mp = that.view.toMap({x: e.x, y: e.y})
                for (var i = 0; i < that.gl.graphics.items.length; i++) {
                    var obj = that.gl.graphics.items[i];
                    if (geometryEngine.contains(obj.geometry, mp)) {
                        console.log(obj)
                        that._yFinal = that._y;
                        that._lastGeometry = obj.geometry
                        that.gl.remove(obj)
                        break;
                    }
                }

            }
            ,
            drawcube_pt_move: function (e) {
                that._y = e.y
                if (that._yFinal != 0) {
                    var symbol = new PolygonSymbol3D({
                        symbolLayers: [new ExtrudeSymbol3DLayer({
                            size: that.view.scale * Math.abs(e.y - that._yFinal) / 1000,  // 100,000 meters in height
                            material: {color: that.fillSymbol.color}
                        })]
                    });
                    that.addGraphic2Map(that._lastGeometry, symbol)
                }

            }
            ,
            drawDash: function (e) {
                that._points.push(e.mapPoint.x)
                that._points.push(e.mapPoint.y)
                that._points.push(e.mapPoint.z)
                var pre = curr = null
                if (that._points.length > 5) {
                    var geo = that.buildThreeGeometry(that._points)
                    pre = curr
                    if (pre)
                        externalRenderers.remove(view, pre);
                    var curr = that.extRenderInstance(geo, this)
                    externalRenderers.add(view, curr);
                }
            }
            ,
            newHead: function () {
                that._newHead = true
                that._headerCollection.push(null)

            }
            ,
            buildThreeGeometry: function (x) {
                var renderCoordinates = [];

                externalRenderers.toRenderCoordinates(view,
                    x, 0, null,
                    renderCoordinates, 0,
                    x.length / 3);

                var geometry = new THREE.Geometry();

                for (var i = 0; i < renderCoordinates.length / 3; i++) {

                    geometry.vertices.push(new THREE.Vector3(renderCoordinates[3 * i], renderCoordinates[3 * i + 1], renderCoordinates[3 * i + 2] + 30000))
                    if (i > 0)
                        geometry.vertices.push(new THREE.Vector3(renderCoordinates[3 * i], renderCoordinates[3 * i + 1], renderCoordinates[3 * i + 2] + 30000))
                }
                geometry.vertices.pop()
                return geometry

            }
            ,
            extRenderInstance: function (geometry, drawInstance) {
                return {
                    renderer: null,     // three.js renderer
                    camera: null,       // three.js camera
                    scene: null,        // three.js scene

                    ambient: null,      // three.js ambient light source
                    sun: null,          // three.js sun light source

                    iss: null,                                                          // ISS model
                    issScale: 40000,                                                    // scale for the iss model
                    issMaterial: new THREE.MeshLambertMaterial({color: 0x38F8FF}),    // material for the ISS model

                    cameraPositionInitialized: false, // we focus the view on the ISS once we receive our first data point
                    positionHistory: [],              // all ISS positions received so far

                    markerMaterial: null,    // material for the markers left by the ISS
                    markerGeometry: null,    // geometry for the markers left by the ISS

                    setup: function (context) {
                        that.renderer = new THREE.WebGLRenderer({
                            context: context.gl,
                            premultipliedAlpha: false
                        });
                        that.renderer.setPixelRatio(window.devicePixelRatio);
                        that.renderer.setSize(context.camera.fullWidth, context.camera.fullHeight);

                        // prevent three.js from clearing the buffers provided by the ArcGIS JS API.
                        that.renderer.autoClearDepth = false;
                        that.renderer.autoClearStencil = false;
                        that.renderer.autoClearColor = false;

                        // The ArcGIS JS API renders to custom offscreen buffers, and not to the default framebuffers.
                        // We have to inject this bit of code into the three.js runtime in order for it to bind those
                        // buffers instead of the default ones.
                        var originalSetRenderTarget = that.renderer.setRenderTarget.bind(that.renderer);
                        that.renderer.setRenderTarget = function (target) {
                            originalSetRenderTarget(target);
                            if (target == null) {
                                context.bindRenderTarget();
                            }
                        }

                        // setup the three.js scene
                        ///////////////////////////////////////////////////////////////////////////////////////

                        that.scene = new THREE.Scene();

                        // setup the camera
                        var cam = context.camera;
                        that.camera = new THREE.PerspectiveCamera(cam.fovY, cam.aspect, cam.near, cam.far);

                        // setup scene lighting
                        that.ambient = new THREE.AmbientLight(0xffffff, 0.5);
                        that.scene.add(that.ambient);
                        that.sun = new THREE.DirectionalLight(0xffffff, 0.5);
                        that.scene.add(that.sun);

                        geometry.computeLineDistances();
                        var line = new THREE.LineSegments(geometry, new THREE.LineDashedMaterial({
                            color: drawInstance.lineSymbol.color.toHex(),
                            dashSize: 60000,
                            gapSize: 30000,
                            linewidth: 1
                        }));
                        var transform = new THREE.Matrix4();
                        that.scene.add(line);
                        context.resetWebGLState();
                    }
                    ,
                    render: function (context) {
                        var cam = context.camera;

                        that.camera.position.set(cam.eye[0], cam.eye[1], cam.eye[2]);
                        that.camera.up.set(cam.up[0], cam.up[1], cam.up[2]);
                        that.camera.lookAt(new THREE.Vector3(cam.center[0], cam.center[1], cam.center[2]));

                        // Projection matrix can be copied directly
                        that.camera.projectionMatrix.fromArray(cam.projectionMatrix);

                        that.renderer.resetGLState();
                        that.renderer.render(that.scene, that.camera);

                        // as we want to smoothly animate the ISS movement, immediately request a re-render
                        externalRenderers.requestRender(view);

                        // cleanup
                        context.resetWebGLState();
                    }
                }
            }
            ,
            ReDrawAllGraphics: function () {
                //根据curMarkers的内容来进行重绘
                var gpLen = that.curMarkers.graphics.length;

                for (var i = 0; i < gpLen; i++) {

                    var item = that.curMarkers.graphics[i];

                    switch (item.type) {
                        case 'point':
                            that.reset('on-the-ground');
                            that.draw_pt_show(item.points[0], true, item);
                            break;
                        case 'pticon':
                            var picUrl = item.icon;
                            that.draw_pticon_show(item.points[0], item.newItem[0]);
                            break;
                        case 'freeline':
                            that.resetOnly('on-the-ground');
                            that.drawFreeLine_show(item.points, true, item);
                            break;
                        case 'line':
                            draw.activate(DrawExt.LINE)
                            break;
                        case 'dashline':
                            draw.activate(DrawExt.DASHLINE)
                            break;
                        case 'polygon':
                            draw.activate(DrawExt.POLYGON)
                            break;
                        case 'circle':
                            draw.activate(DrawExt.CIRCLE)
                            break;
                        case 'curve':
                            draw.activate(DrawExt.CURVE)
                            break;
                        case 'freehandarrow':
                            that.resetOnly('on-the-ground');
                            that.drawFreeHandArrow_show(item.points, true, true, item);
                            break;
                        case 'freepoly':
                            draw.activate(DrawExt.BEZIER_POLYGON)
                            break;
                        case 'Bezierline':
                            draw.activate(DrawExt.BEZIER_CURVE)
                            break;
                        case 'multihead':
                            that.resetOnly('on-the-ground');
                            that.drawMultiHead_pt_show(item.points, true, true, item)
                            break;
                        case 'cube':
                            draw.activate(DrawExt.CUBE)
                            break;
                        case 'trackarrow':
                            that.resetOnly('on-the-ground');
                            that.drawAttackArrow_pt_show(item.points, true, true, item)
                            break;
                        case 'cleardraw':
                            draw.clearAllGraphic();
                            break;
                        case 'fireline':
                            that.resetOnly('on-the-ground');
                            that.drawbezierLineFire_pt_show(item.points, item.positions, true, true, item)
                            break;
                        case 'fireArea':
                            that.resetOnly('on-the-ground');
                            that._specialSymbol = new SimpleFillSymbol({
                                color: [30, 30, 30, 0.8],
                                outline: {
                                    color: [255, 0, 0],
                                    width: 2
                                }
                            });
                            that.drawFreePolygon_pt_show(item.points, false, true, item)
                            break;
                        case 'demilitarizedZone':
                            that.resetOnly('on-the-ground');
                            //定义不现的填充
                            that._specialSymbol = new SimpleFillSymbol({
                                color: [0, 255, 0, 0.8],
                                outline: {
                                    color: [0, 255, 0],
                                    width: 2
                                }
                            })
                            that.drawPolygon_pt_show(item.points, false, true, item)
                            break;
                        case 'fireSprawl':
                            draw.showSprawlDlg();
                    }
                }
            }

            //显示火势蔓延对话框
            ,
            showSprawlDlg: function () {

                $("#fireSprawlParmDlg").modal('show');

            } //end showSprawlDlg

            //火势蔓延参数结果展示
            ,
            showSprawlRs: function () {
                //（1）取参数
                var paraWeather = parseFloat($("#paraWeather").val());
                var paraVegetation = parseFloat($("#paraVegetation").val());
                var paraTemperature = parseFloat($("#paraTemperature").val());
                var paraHumidity = parseFloat($("#paraHumidity").val());
                var paraWindDirect = parseFloat($("#paraWindDirect").val());
                var paraWindPower = parseFloat($("#paraWindPower").val());
                var paraTerrin = parseFloat($("#paraTerrin").val());
                var paraTime = parseFloat($("#paraTime").val());

                var paraObj = {
                    paraWeather: paraWeather,
                    paraVegetation: paraVegetation,
                    paraTemperature: paraTemperature,
                    paraHumidity: paraHumidity,
                    paraWindDirect: paraWindDirect,
                    paraWindPower: paraWindPower,
                    paraTerrin: paraTerrin,
                    paraTime: paraTime

                };

                //(2)代入计算

                that.ComputerFireArea(that._lastGeometry, that._lastPoint, paraObj);

                $("#fireSprawlParmDlg").modal('hide');

            } //end showSprawlDlg
            ,
            ComputerFireArea: function (areaGeo, centerPoint, paraObj) {
                //计算新的火情区域，areaGeo为火情区域，centerPoint为中心点，paraObj为参数
                if (paraObj.paraWindPower <= 2) {
                    //按无风处理，直接蔓延

                    //computerNewDist

                    var len = areaGeo.rings[0].length;
                    var newRing = [];

                    for (var i = 0; i < len; i++) {
                        //(1)从area的rings中取出所有的点

                        var item = areaGeo.rings[0][i];

                        //(2)循环处理从中心点开始到指定点的矢量
                        //经纬度转换
                        var newItem = webMercatorUtils.xyToLngLat(item[0], item[1]);

                        var newPoint = MathStuff.computerNewDist(centerPoint.longitude
                            , centerPoint.latitude, newItem[0], newItem[1], paraObj);

                        //(3)返回新的 rings并显示

                        var pointI = webMercatorUtils.lngLatToXY(newPoint.lng, newPoint.lat);

                        newRing.push(pointI);

                    }

                    var polygon = new Polygon({
                        rings: [newRing],
                        spatialReference: that.view.spatialReference
                    })
                    that.addGraphic2Map(polygon, that.fillSymbol)

                }
                ;

            }

            ,
            deleteFireLine: function () {
                //测试删除火线
                //注意此时的groupkey是死的
                timerAnimate.removeQueueByGroupKey(that._groupKey);
            }

        })
        lang.mixin(draw, {
            POINT: "point",
            LINE: "line",
            POLYGON: "polygon",
            CIRCLE: "circle",
            CURVE: "curve",
            BEZIER_CURVE: "beziercurve",
            BEZIER_POLYGON: "bezierpolygon",
            FREEHAND_ARROW: "freehandarrow",
            MULTIHEAD: 'multihead',
            DASHLINE: 'dashline',
            FREELINE: 'freeline',
            CUBE: 'cube',
            FIRELINE: 'fireline',
            PTICON: 'pticon',
            FIREAREA: 'fireArea',
            DMZ: 'demilitarizedZone',
            ATTACK_ARROW: 'trackarrow'
        })
        lang.mixin(draw, {
            messageTemplate: {
                singleClickAdd: '<b>单击</b> 增加一个点.',
                singleIconAdd: '<b>单击</b>增加$$',
                dblclickend: '<b>双击</b> 结束绘制',
                clickdblclick: '<b>单击</b> 增加一中间点<br/><b>双击</b> 结束',
                clickdblright: '<b>单击</b> 增加一中间点<br/><b>右击</b> 增加一个新箭头 <br/><b>双击</b> 结束绘制.',
                singleClickStart: '<b>单击</b> 开始绘制',
                clickAddSecond: '<b>单击</b> 加入中间点',
                clickCubic: '<b>Single</b> click to cubic a polygon'
            }
        })
        return draw;

    })
